package com.yycx.module.bus.provider.controller;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.vo.OrderCustomReturn;
import com.yycx.module.bus.provider.service.BusPaymentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yycx.common.mybatis.model.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RestController;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;


/**
 * 订单明细控制器
 *
 * @author flyme
 * @date 2020-08-19
 */
@RestController
@RequestMapping("/bus/bod/")
public class BusOrderDetailsController extends BaseController<BusOrderDetailsService, BusOrderDetails> {

    @Autowired
    private BusPaymentService paymentService;

    @ApiOperation(value = "订单明细-分页列表", notes = "订单明细分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }


    @ApiOperation(value = "订单明细-列表", notes = "订单明细列表")
    @GetMapping(value = "selectOrderInfoDetails")
    public ResultBody selectOrderInfoDetails(@RequestParam(required = false) Map params) {
        List<EntityMap> list = bizService.selectOrderInfoDetails(params);
        return ResultBody.ok(list);
    }

    @ApiOperation(value = "订单明细-列表", notes = "订单明细列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "订单回款明细-列表", notes = "订单回款明细列表")
    @GetMapping(value = "returnList")
    public ResultBody returnList(@RequestParam(required = false) Long orderId) {
        List<EntityMap> list = paymentService.getOrderReturnDetails(orderId);
        return ResultBody.ok(list);
    }


    @ApiOperation(value = "订单明细-添加", notes = "添加订单明细")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "订单明细-更新", notes = "更新订单明细")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "订单明细-删除", notes = "删除订单明细")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "订单明细-详情", notes = "订单明细详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }


    @ApiOperation(value = "拆分订单", notes = "拆分订单")
    @PostMapping(value = "chaiFen")
    public ResultBody chaiFen(OrderCustomReturn orderCustomReturn, String fileIds) {
        return bizService.chaiFen(orderCustomReturn,fileIds);
    }



    @ApiOperation(value = "退货补货", notes = "退货补货")
    @PostMapping(value = "addOrderDetails")
    public ResultBody addOrderDetails(@RequestParam Long orderDetailsId, @RequestParam String saleDate, @RequestParam BigDecimal saleNum, @RequestParam Integer saleType) {
        return bizService.addReturnOrderDetails(orderDetailsId, saleType, saleNum, saleDate);
    }

    @ApiOperation(value = "票折", notes = "票折")
    @PostMapping(value = "discount")
    public ResultBody discount(@RequestParam Long orderDetailsId, @RequestParam BigDecimal saleAmount) {
        return bizService.addDiscountDetails(orderDetailsId, saleAmount);
    }

    @ApiOperation(value = "删除退补记录", notes = "删除订单明细")
    @PostMapping(value = "deleteTuibu")
    public ResultBody deleteTuibu(@RequestParam Long orderDetailsId) {
        return bizService.deleteTuibu(orderDetailsId);
    }

    @ApiOperation(value = "删除明细表缓存", notes = "删除明细表缓存")
    @PostMapping(value = "clearExportData")
    public ResultBody clearExportData() {
        String redisKey = "report:" + OpenHelper.getUserId();
        redisUtils.del(redisKey);
        return ResultBody.msg("缓存删除成功");
    }

    @ApiOperation(value = "更新批号", notes = "更新批号")
    @PostMapping(value = "updateLotNo")
    public ResultBody updateLotNo(@RequestParam(value = "drugsLotId") Long drugsLotId, @RequestParam(value = "orderDetailsId") Long orderDetailsId) {
        return bizService.updateLotNo(drugsLotId, orderDetailsId);
    }


    @ApiOperation(value = "撤销回款", notes = "撤销回款")
    @PostMapping(value = "revoke")
    public ResultBody revoke(@RequestParam(value = "returnId") Long returnId) {
        return bizService.revoke(returnId);
    }

    @ApiOperation(value = "更新认款月份", notes = "更新认款月份")
    @PostMapping(value = "updateRenkuan")
    public ResultBody updateRenkuan(String renkuanDate,String supplierRetDate2, Long orderDetailsId) {
        return bizService.updateRenkuan(renkuanDate,supplierRetDate2, orderDetailsId);
    }

    @ApiOperation(value = "更新回款", notes = "更新回款")
    @PostMapping(value = "updateHuikuan")
    public ResultBody updateHuikuan(String returnDate,BigDecimal returnAmount, Long orderDetailsId) {
        return bizService.updateHuikuan(returnDate,returnAmount, orderDetailsId);
    }


    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        bizService.export(params, request, response, "开票明细", "开票明细", "yesInvoiceDetailsPageListHandler");
    }

    @PostMapping(value = "exportSettleDetails")
    public void exportSettleDetails(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        bizService.export(params, request, response, "结算明细", "结算明细", "yesSettlePageListHandler");
    }

    /**
     * 按年月周日类型统计销售额和订单量
     *
     * @param params
     * @return
     */
    @GetMapping(value = "totalSaleByType")
    public ResultBody totalSaleByType(@RequestParam(required = false) Map params) {
        return bizService.totalSaleByType(params);
    }


}
